package com.example.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Device;
import com.example.springboot.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/echarts")
public class EchartsController {
    @Autowired
    private DeviceMapper deviceMapper;


//    @GetMapping("/example")
//    public Result get(){
//        Map<String,Object> map = new HashMap<>();
//        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
//        map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
//        return Result.success(map);
//    }

    @GetMapping("/purchase")
    public Result purchase() throws ParseException { //分季度统计采购数量
        List<Device> list = deviceMapper.findAll();
        int q1 = 0; //第一季度
        int q2 = 0; //第二季度
        int q3 = 0; //第三季度
        int q4 = 0; //第四季度
        for(Device device : list){
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date buyDate = dateFormat.parse(device.getBuyDate());//将string型的buyDate转换成Date型
            Quarter quarter = DateUtil.quarterEnum(buyDate);
            switch (quarter) {
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;
                default: break;
            }
        }
        return Result.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }

    @GetMapping("/classify")
    public Result classify(){ //各类型设备采购数量统计
        List<Device> list = deviceMapper.findAll();
        int s1 = 0; //电教设备
        int s2 = 0; //文体设备
        int s3 = 0; //炊事器械
        int s4 = 0; //其它
        for(Device device : list){
            String type = device.getDeviceType();
            switch (type) {
                case "电教设备": s1 += 1; break;
                case "文体设备": s2 += 1; break;
                case "炊事器械": s3 += 1; break;
                case "其它": s4 += 1; break;
                default: break;
            }
        }
        return Result.success(CollUtil.newArrayList(s1,s2,s3,s4));
    }

    @GetMapping("/store")
    public Result store() throws ParseException { //各设备库存数量统计
        List<Device> list = deviceMapper.findAll();
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        int q5 = 0;
        int q6 = 0;
        int q7 = 0;
        int q8 = 0;
        int q9 = 0;
        int q10 = 0;
        int q11 = 0;
        int q12 = 0;
        for(Device device : list){
           String name = device.getDeviceName();
            switch (name) {
                case "电脑": q1 += 1; break;
                case "麦克风": q2 += 1; break;
                case "音响": q3 += 1; break;
                case "投影仪": q4 += 1; break;
                case "签字笔": q5 += 1; break;
                case "笔记本": q6 += 1; break;
                case "黑板": q7 += 1; break;
                case "微波炉": q8 += 1; break;
                case "烤箱": q9 += 1; break;
                case "冰柜": q10 += 1; break;
                case "榨汁机": q11 += 1; break;
                case "插线板": q12 += 1; break;
                default: break;
            }
        }
        return Result.success(CollUtil.newArrayList(q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12));
    }
}
